﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace StuApart
{
    public partial class login : Form
    {
        public login()
        {
            InitializeComponent();
        }
        // 下载于www.mycodes.net
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Length == 0) {
                MessageBox.Show("用户名不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                textBox1.Focus();
                return;
            }
            if (textBox2.Text.Length == 0)
            {
                MessageBox.Show("密码不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                textBox2.Focus();
                return;
            }
            SqlConnection conn = new SqlConnection(Common.DataBase.connectionString);
            Common.DataBase mydb = new Common.DataBase();
            string sqlStr = "SELECT * FROM Admin WHERE adminName='" + textBox1.Text.Trim() + "'";
            sqlStr += "AND adminPassword='" + textBox2.Text.Trim() + "'";
            SqlDataReader dr = mydb.getDataReader(sqlStr);
            if (dr.HasRows)
            {
                Common.DataBase.userName = textBox1.Text.Trim();
                Common.DataBase.userPassword = textBox2.Text.Trim();
                dr.Close();
                conn.Close();
                frmMain frmmain = new frmMain();
                frmmain.Show();
                this.Hide();
            }
            else
            {
                if (conn != null)
                    conn.Close();
                MessageBox.Show("用户名或密码不正确，请重新输入", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBox1.Clear();
                textBox2.Clear();
                textBox1.Focus();
            }
        }


        // 下载于www.mycodes.net
        private void button3_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
            textBox2.Clear();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Length == 0)
            {
                MessageBox.Show("用户名不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                textBox1.Focus();
                return;
            }
            if (textBox2.Text.Length == 0)
            {
                MessageBox.Show("密码不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                textBox2.Focus();
                return;
            }
            SqlConnection conn = new SqlConnection(Common.DataBase.connectionString);
            Common.DataBase mydb = new Common.DataBase();
            string sqlStr = "SELECT * FROM users WHERE users='" + textBox1.Text.Trim() + "'";
            sqlStr += "AND password='" + textBox2.Text.Trim() + "'";
            SqlDataReader dr = mydb.getDataReader(sqlStr);
            if (dr.HasRows)
            {
                Common.DataBase.userName = textBox1.Text.Trim();
                Common.DataBase.userPassword = textBox2.Text.Trim();
                dr.Close();
                conn.Close();
                frmStudent frmmain = new frmStudent();
                frmmain.Show();
                this.Hide();
            }
            else
            {
                if (conn != null)
                    conn.Close();
                MessageBox.Show("用户名或密码不正确，请重新输入", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBox1.Clear();
                textBox2.Clear();
                textBox1.Focus();
            }
        }

        /*private void textBox1_KeyPress(object sender, KeyEventArgs e)
        {
            if (e.KeyChar == 13)
                textBox2.Focus();
        }
        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
                buttonOK.Focus();
        }
        private void login_FormClosing(object sender, KeyPressEventArgs e)
        {
            Application.Exit();
        }*/
    }
}
